package itsm.isperp.module.controller.project;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjCostTemplate;
import itsm.isperp.module.service.project.ProjCostTemplateService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 项目预算模板控制器
 * 
 * @author lizx
 * @date 2013-2-26
 * @version V1.0
 */
@Controller
@RequestMapping("project/costTemplate")
public class ProjCostTemplateController extends
		TreeNodeController<ProjCostTemplate> {

	@Autowired
	protected ProjCostTemplateService projCostTemplateService;

	@Override
	public ProjCostTemplateService getService() {
		return this.projCostTemplateService;
	}

	@Override
	public String getPageTitle() {
		return "项目预算模板";
	}

	@Override
	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(ProjCostTemplate model, HttpServletRequest req) {
		model.setName(model.getSubjectName() + "┆" + model.getCode());

		if (!model.isFixAmount()) {
			model.setCalculateFormula("");
		}

		this.getService().saveOrUpdate(model, req);
		return ResultMessage.newInstance(true, null, model.getId());
	}

	/**
	 * 取得树形结构的的json数据
	 * 
	 * @return 菜单的json数据
	 */
	@RequestMapping("getCostTree")
	@ResponseBody
	@Deprecated
	public List<? extends ITreeNode> getCostTree(String projectId,
			String parentId) {

		return this.getService().getCostTree(projectId, parentId);
	}

	/**
	 * 模板管理
	 * 
	 * @return
	 */
	@RequestMapping("listManage")
	public ModelAndView listManage() {
		return this.getModelAndView();
	}

	/**
	 * 列出所有的模
	 * 
	 * @param req
	 *            数据请求
	 * @return 列表数据
	 */
	@RequestMapping("listTemplate")
	@ResponseBody
	public DataResponse<ProjCostTemplate> listTemplate(JqGridRequest req) {
		req.setSearchString(" parent_id ='' OR parent_id IS NULL ");
		return this.getService().findAll(req);
	}

	/**
	 * 模板编辑
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑界面
	 */
	@RequestMapping("rootEdit")
	public ModelAndView rootEdit(String id) {
		ProjCostTemplate model = null;

		if (StringUtils.isEmpty(id)) {
			model = new ProjCostTemplate();
			model.setId(HibernateUUIDGenerator.generate());
			model.setCode("SUM");
			model.setName(model.getSubjectName() + "┆SUM");
			model.setFixAmount(true);
			model.setParentId("");
			model.setOpen(true);
		} else {
			model = this.getService().findOne(id);
		}
		ModelAndView mv = this.getModelAndView();

		mv.addObject("model", model);
		return mv;
	}

	/**
	 * 删除模板
	 * 
	 * @param id
	 *            唯一标识
	 * @return 结果信息
	 */
	@RequestMapping("deleteTemplate")
	@ResponseBody
	public ResultMessage deleteTemplate(String id) {
		// 检查模板是否有被使用
		// 如果有被使用则不能被删除
		if (this.getService().templateUseInProject(id)) {
			return ResultMessage.failure("此模板在项目预算中被使用，不能被删除！");
		}

		this.getService().delete(id);
		return ResultMessage.success();
	}

	/**
	 * 模板项编辑
	 * 
	 * @param id
	 *            模板id
	 * @return 模板项编辑界面
	 */
	@RequestMapping("treeEdit")
	public ModelAndView treeEdit(String id) {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("parentId", id);
		return mv;
	}

	/**
	 * 模板管理
	 * 
	 * @return
	 */
	@RequestMapping("preview")
	public ModelAndView preview(String id) {
		ModelAndView mv = this.getModelAndView();

		mv.addObject("costTemplate", this.getService().getCostTreeTable(id)
				.renderHtml());

		return mv;
	}
}
